package blt.app_core.api.base.callback;

import blt.app_core.api.base.CoreManager;
import blt.app_core.api.base.IBaseApi;
import blt.app_core.api.base.bean.State;
import blt.app_core.log.Logger;
import retrofit2.Call;
import retrofit2.Response;

/**
 * data为对象
 * 2017/6/18.
 */

public abstract class StateCallback<T> extends BaseCallback<State<T>> {
    public State state;

    @Override
    public void onResponse(Call<State<T>> call, Response<State<T>> response) {
        super.onResponse(call, response);
        state = response.body();
        if (state == null) {
            return;
        }
        if (state.errcode != 200) {
            CoreManager.notifyClients(IBaseApi.class, IBaseApi.ERROR_MSG, state.errmsg);
        } else {
            T data = response.body().data;
            onSuccess(data);
        }
    }

    protected abstract void onSuccess(T data);

    @Override
    public void onFailure(Call<State<T>> call, Throwable t) {
        super.onFailure(call, t);
        CoreManager.notifyClients(IBaseApi.class, IBaseApi.ERROR_MSG, FAILED);
    }

    //    @Override
//    public void onResponse(Call<State> call, Response<State> response) {
//        super.onResponse(call, response);
////        Logger.d("****onResponse*****");
//        State state = response.body();
//        if (state == null) {
//            return;
//        }
//        if (state.errcode != 200) {
//            CoreManager.notifyClients(IBaseApi.class, IBaseApi.ERROR_MSG, state.errmsg);
//        } else {
//            onSuccess(response.body());
//        }
//    }
//
//    @Override
//    public void onFailure(Call<State> call, Throwable t) {
//        super.onFailure(call, t);
//        CoreManager.notifyClients(IBaseApi.class, IBaseApi.ERROR_MSG, FAILED);
//    }

//    public abstract void onSuccess(State body);


}
